package screen;

import game.Configurations;
import game.FontLoader;
import images.ImageInfo;
import java.awt.Color;
import java.util.ArrayList;
import org.lwjgl.input.Keyboard;
import org.newdawn.slick.TrueTypeFont;
import screen.CaixaTexto.TipoInput;

/**
 *
 * @author Luan Nico
 */
public class Dialog extends ObjetoTela {
    private int codigo;
    
    private TipoInput input;
    private String[] linhas, buttons;
    private int[] pbtn;
    private int selectedItem;
    
    private Box box;
    private CaixaTexto t;
    private TrueTypeFont fonte;
    
    public Dialog(int w, String texto) {
        this(w, texto, new String[] {"Ok"});
    }
    
    public Dialog(int w, String texto, String[] buttons) {
        this(w, texto, null, buttons);
    }
    
    public Dialog(int w, String texto, TipoInput input, String[] buttons) {
        this(w, texto, input, "", buttons);
    }

    public Dialog(int w, String texto, TipoInput input, String defaultValue, String[] buttons) {
        this(-1, w, texto, input, defaultValue, buttons);
    }
    
    public Dialog(int codigo, int w, String texto, TipoInput input, String defaultValue, String[] buttons) {
        super((Configurations.SCREEN_WIDTH - w)/2, 10);
        this.codigo = codigo;
        int width = w - 10;
        int pos = 0;
        String txt = "";
        ArrayList<String> lns = new ArrayList<>();
        this.fonte = FontLoader.TEXT;
        this.input = input;
        while (pos < texto.length()) {
            if (fonte.getWidth(txt + texto.charAt(pos)) <= width)
                txt += texto.charAt(pos);
            else {
                lns.add(txt);
                txt = "" + texto.charAt(pos);
            }
            pos++;
        }
        if (!txt.isEmpty())
            lns.add(txt);
        
        this.buttons = buttons;
        this.pbtn = new int[buttons.length]; //buttons' positions
        int total = -5;
        for (int i = 0; i < buttons.length; i++)
            total += this.fonte.getWidth(buttons[i]) + 5;
        this.pbtn[0] = (w - total)/2;
        for (int i = 1; i < buttons.length; i++)
            this.pbtn[i] = this.pbtn[i - 1] + 5 + this.fonte.getWidth(buttons[i - 1]);
        this.linhas = lns.toArray(new String[lns.size()]);
        int h = 10 + (this.linhas.length + (input == null ? 1 : 3)) * (this.fonte.getLineHeight() + 4);
        y = (Configurations.SCREEN_HEIGHT - h)/2;
        if (this.input != null) {
            this.selectedItem = -1;
            this.t = new CaixaTexto(x + 5, y + 5 + this.linhas.length * (this.fonte.getLineHeight() + 4), defaultValue, w - 10, input);
            this.t.selected = true;
        } else
            this.selectedItem = 0;
        this.box = new Box(x, y, w, h);
    }

    public int getCodigo() {
        return codigo;
    }
    
    public void parse(int key, char c) {
        if (t != null && key == Keyboard.KEY_UP) {
            this.selectedItem = -1;
            this.t.selected = true;
        } else if (t != null && key == Keyboard.KEY_DOWN) {
            this.selectedItem =  0;
            this.t.selected = false;
        }
        
        if (selectedItem == -1)
            t.parse(key, c);
        else {
            if (key == Keyboard.KEY_RIGHT || key == Keyboard.KEY_D) {
                if (selectedItem < pbtn.length - 1)
                    selectedItem++;
            } else if (key == Keyboard.KEY_LEFT || key == Keyboard.KEY_A) {
                if (selectedItem > 0)
                    selectedItem--;
            }
        }
    }
    
    public boolean validate() {
        if (t == null)
            return true;
        return t.validate();
    }
    
    public String getInput() {
        if (t == null)
            return null;
        return t.getTexto();
    }
    
    public int getSelectedItem() {
        return selectedItem;
    }
    
    @Override
    public void desenhar() {
        box.desenhar();
        for (int i = 0; i < linhas.length; i++)
            fonte.drawString(x + 5, y + 5 + i * (fonte.getLineHeight() + 4), linhas[i]);
        //draw input
        if (input != null)
            t.desenhar();
        
        if (selectedItem != -1)
            ImageInfo.drawColorSquare(x + pbtn[selectedItem] - 3, y + (linhas.length + (input == null ? 0 : 2)) * (fonte.getLineHeight() + 4), fonte.getWidth(buttons[selectedItem]) + 4, fonte.getLineHeight() + 10, Color.GRAY);
        //draw buttons
        for (int i = 0; i < buttons.length; i++)
            fonte.drawString(x + pbtn[i], y + 5 + (linhas.length + (input == null ? 0 : 2)) * (fonte.getLineHeight() + 4), buttons[i]);
    }
}
